package org.example.practiceexamples.service;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.Lab;
import org.example.practiceexamples.exception.Code;
import org.example.practiceexamples.exception.XException;
import org.example.practiceexamples.repository.AppointmentRepository;
import org.example.practiceexamples.repository.CourseRepository;
import org.example.practiceexamples.repository.LabRepository;
import org.example.practiceexamples.repository.UserRepository;
import org.example.practiceexamples.tools.JsonTemp;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class ManageService {

    private final UserRepository userRepository;
    private final LabRepository labRepository;
    private final AppointmentRepository appointmentRepository;
    private final CourseRepository courseRepository;

    @Transactional
    public Lab findLabById(String  uid,String lid) {
        if(!labRepository.findLabById(lid).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        return labRepository.findLabById(lid);
    }

    @Transactional
    public Lab findLabByName(String  uid,String name) {
        if(!labRepository.findLabByName(name).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        return labRepository.findLabByName(name);
    }

    @Transactional
    public List<Appointment> findAppointmentsByLid(String uid,String lid){
        if(!labRepository.findLabById(lid).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        return labRepository.findLabAppointmentByLid(lid).getAppointments();
    }

    @Transactional
    public List<Appointment> findAppointmentsByLname(String uid,String name){
        if(!labRepository.findManageByName(name).equals(userRepository.findUserByUid(uid))){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        return labRepository.findLabAppointmentByName(name).getAppointments();
    }


    @Transactional
    public void deleteLabByName(String uid,String name){
        if(!labRepository.findLabByName(name).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        labRepository.deleteLabByName(name);
    }


    @Transactional
    public void deleteLabById(String uid,String lid){
        if(!labRepository.findLabById(lid).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        labRepository.deleteLabById(lid);
    }

    @Transactional
    public void updateLabDescriptionByName(String uid,String description,String name){
        if(!labRepository.findLabByName(name).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        labRepository.updateDescriptionByName(description,name);
    }

    @Transactional
    public void updateStateByName(String uid,int state,String name){
        if(!labRepository.findLabByName(name).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        labRepository.updateStateByName(state,name);
    }

    @Transactional
    public void updateQuantityByName(String uid,int quantity,String name){
        if(!labRepository.findLabByName(name).getManage().equals(userRepository.findUserByUid(uid).getName())){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        labRepository.updateQuantityByName(quantity,name);
    }

    @Transactional
    public void deleteAppointmentById(String uid,String aid) {
        Appointment appointment = appointmentRepository.findAppointmentById(aid);
        String tid = JsonTemp.toObject(appointment.getTeacher()).getId();
        if(tid!=uid){
            throw XException.builder().code(Code.NO_USER).message(Code.NO_USER.getMessage()).build();
        }
        appointmentRepository.deleteAppointmentById(aid);
    }

}
